package com.atguigu.cloud.config.securityConfig.UserDetailsManager;/**
 * @Auter 李孝在
 * @Date 2024/11/18
 */

import com.atguigu.cloud.entities.User;
import com.atguigu.cloud.mapper.AdminMapper;
import com.atguigu.cloud.mapper.MerchantMapper;
import com.atguigu.cloud.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.config.securityConfig
 * @className: DBUserD
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/18 00:18
 * @version: 1.0
 */
//这个是否可以直接写在servlet中呢?
public class DBUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {

    @Resource
    private UserMapper userMapper;


    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return null;
    }

    @Override
    public void createUser(UserDetails user) {

    }

    @Override
    public void updateUser(UserDetails user) {

    }

    @Override
    public void deleteUser(String username) {

    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    @Override
    public boolean userExists(String username) {
        return false;
    }

    //数据库中获取用户数据
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name",username);
        User user=userMapper.selectOne(userQueryWrapper);

        if (user==null){
            throw new UsernameNotFoundException(username);
        }else {

            Collection<GrantedAuthority> authorities =new ArrayList<>();
            return new org.springframework.security.core.userdetails.User(
                    user.getName(),
                    user.getPassword(),
                    true,//是否激活:?
                    true,//账号过期
                    true,//密码过期
                    true,//锁定:?
                    authorities//权限角色
            );

        }


    }
}
